#include <stdio.h>
#include "../includes/myheap.h"

int mycmp(void *a, void *b) {
    return *((int *) a) - *((int *) b);
}

int main(int argc, char **argv) {
    printf("myheap test start\n");
    int size = 10;
    int arr[] = {7, 1, 3, 23, 634, 11, 99, 123, 2121, 1198};
    int **arr1 = malloc(sizeof(int *) * size);
    for (int i = 0; i < size; i++)
    {
        arr1[i] = arr + i;
    }
    myheap *h1 = heap_create_arr((void **)arr1, size, size, mycmp);
    for (int i = 0; i < size; i++)
    {
        int *r = heap_remove(h1);
        printf("heap root value: %d len: %d\n", *r, h1->len);
    }
    printf("\n\n");
    for (int i = 0; i < size; i++)
    {
        heap_insert(h1, arr + i);
    }
    for (int i = 0; i < size; i++)
    {
        int *r = heap_remove(h1);
        printf("heap root value: %d len: %d\n", *r, h1->len);
    }
    heap_destory(h1, 1);

    // heap sort
    int **arr2 = malloc(sizeof(int *) * size);;
    for (int i = 0; i < size; i++)
    {
        arr2[i] = arr + i;
    }
    heap_sort(arr2, size, mycmp);
    for (int i = 0; i < size; i++)
    {
        printf("after heap sort: %d\n", *arr2[i]);
    }

    // heap set
    printf("start heap set\n");
    int val = 100;
    myheap *h2 = heap_create_arr((void **)arr2, size, size, mycmp);
    heap_set(h2, 5, &val);
    
    for (int f = 0; f < size; f++)
    {
        int *r = heap_remove(h2);
        printf("heap root value: %d len: %d\n", *r, h2->len);
    }
    printf("after heap set and remove\n");
    heap_destory(h2, 1);

    return 0;
}